/*
XBaseTools
Copyright (C) 2008 Matthew Perry

This library/program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Collections.Generic;
using BaseTools.IO;
using BaseTools.Configuration;
using BaseTools.Configuration.Parsers;
using BaseTools.Helpers;

namespace SourceTools.IO
{
	/// <summary>
	/// Gets game information
	/// </summary>
	public class GameInfo
	{
		private string giDirectory, giGame;
		
		/// <summary>
		/// Creates a new GameInfo
		/// </summary>
		/// <param name="directory">Base Source directory</param>
		/// <param name="game">Base Game directory</param>
		public GameInfo(string directory, string game)
		{
			giDirectory = directory;
			giGame = game;
		}
		
		/// <summary>
		/// Creates a SourceGameInfo object from the GameInfo.txt
		/// </summary>
		public SourceGameInfo SourceInfo
		{
			get
			{
				return new SourceGameInfo(IOHelper.RelativeTo(giDirectory, giGame) + "\\gameinfo.txt");
			}
			set
			{
				new SourceConfig().SaveConfiguration(IOHelper.RelativeTo(giDirectory, giGame) + "\\gameinfo.txt", value.GenerateConfig());
			}
		}
		
		/// <summary>
		/// Creates a virtual source file system for this game and any included in GameInfo.txt
		/// </summary>
		/// <returns>A SFS of the game</returns>
		public SourceFileSystem VirtualFileSystem()
		{
			return new SourceFileSystem(SourceInfo.SearchPaths, giDirectory, giGame);
		}
		
		/// <summary>
		/// Creates a virtual source file system for this game
		/// </summary>
		/// <param name="checkFile">Whether to include games included in GameInfo.txt</param>
		/// <returns>A SFS of the game</returns>
		public SourceFileSystem VirtualFileSystem(bool checkFile)
		{
			if (checkFile)
			{
				return new SourceFileSystem(SourceInfo.SearchPaths, giDirectory, giGame);
			}
			else
			{
				return new SourceFileSystem(new SearchPath[] {new SearchPath(SearchPathType.GamePath, giGame)}, giDirectory, giGame);
			}
		}
		
		/// <summary>
		/// Lists the maps in this game
		/// </summary>
		/// <param name="checkFile">Also include maps from imported games</param>
		/// <returns>A list of map names in this game</returns>
		public List<string> ListMaps(bool checkFile)
		{
			List<string> mapList = new List<string>(), bspList;
			List<string> checkList = new List<string>();
			
			bspList = this.VirtualFileSystem(checkFile).ListFiles("maps", "*.bsp");
			
			foreach(string f in bspList)
			{
				if (!checkList.Contains(f.ToLower()))
				{
					checkList.Add(f.ToLower());
					mapList.Add(f.Substring(0, f.Length-4));
				}
			}
			
			return mapList;
		}
		
		/// <summary>
		/// Lists the maps in this game and imported games
		/// </summary>
		/// <returns>A list of maps in this game and imported games</returns>
		public List<string> ListMaps()
		{
			return ListMaps(true);
		}
	}
}
